package guoanmaker.operator.business.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.MessageType;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.MessageTypeRepository;

@Service
public class MessageTypeService {
	
	@Autowired
	private   MessageTypeRepository messageTypeRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	
	public   Map<String, Object>   MessageForBusiness(String businessid){
		Map<String, Object>  map =new HashMap<>();
		List<MessageType> list=	messageTypeRepository.findbyBusinessidAndIsreadNotEqual1(businessid);
	    map.put("list",list);
	    int  item=messageTypeRepository.findByBusinessidNum(businessid);
	    map.put("notReadnum",item);	
		return  map;
	} 
	
	@Transactional
	public  ReturnResult  MessageSetIsread(String businessid){
		ReturnResult returnResult=new ReturnResult();
       List<MessageType> messages=messageTypeRepository.findbyBusinessidAndIsreadNotEqual1(businessid);
       if(messages.size()>0){
    	   String jpql = "UPDATE MessageType p set p.isread=1 where p.businessid =:businessid"; 
    	   Query query = manager.createQuery(jpql); 
    	   query.setParameter("businessid",businessid); 
    	   query.executeUpdate();
    	   returnResult.setKey("success");
    	   returnResult.setValue("已读");
       }else{
    	   returnResult.setKey("error");
    	   returnResult.setValue("无数据");
       }
		return returnResult;
	}
	
	

}
